package requests

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/spf13/cast"

	"github.com/goravel/framework/contracts/validation"
	. "goravel/app/models/common"
)

type UserRequest struct {
	Name            string     `form:"name" json:"name"`
	Mobile          string     `form:"mobile" json:"mobile"`
	Password        string     `form:"password" json:"password"`
	ConfirmPassword string     `form:"confirm_password" json:"confirm_password"`
	Area            string     `form:"area" json:"area"`
	Contact         string     `form:"contact" json:"contact"`
	ContactMobile   string     `form:"contact_mobile" json:"contact_mobile"`
	Address         string     `form:"address" json:"address"`
	IsAdmin         int        `form:"is_admin" json:"is_admin"`
	IdCard          string     `form:"id_card" json:"id_card"`
	ControlArr      ControlArr `form:"control_arr" json:"control_arr"`
	Pid             int        `form:"pid" json:"pid"`
}

func (r *UserRequest) Authorize(ctx http.Context) error {
	return nil
}

func (r *UserRequest) Rules(ctx http.Context) map[string]string {
	return map[string]string{
		"name":             "required|min_len:2|max_len:20",
		"mobile":           "required|len:11",
		"password":         "required|min_len:6",
		"confirm_password": "required|min_len:6",
		"area":             "required",
		"contact":          "required",
		"contact_mobile":   "required|len:11",
		"address":          "required",
		"id_card":          "required|len:18",
		"control_arr":      "required",
		"pid":              "-",
		"is_admin":         "-",
	}
}

func (r *UserRequest) Messages(ctx http.Context) map[string]string {
	return map[string]string{
		"name.required":             "请输入姓名",
		"name.min_len":              "姓名长度不能小于2",
		"name.max_len":              "姓名长度不能大于20",
		"mobile.required":           "请输入手机号",
		"mobile.len":                "手机号格式不正确",
		"password.required":         "请输入密码",
		"password.min_len":          "密码长度不能小于6",
		"confirm_password.required": "请输入确认密码",
		"confirm_password.min":      "确认密码长度不能小于6",
		"area.required":             "请输入地区",
		"contact.required":          "请输入联系人",
		"contact_mobile.required":   "请输入联系人手机号",
		"contact_mobile.len":        "联系人手机号格式不正确",
		"address.required":          "请输入详细地址",
		"id_card.required":          "请输入身份证号",
		"id_card.len":               "身份证号格式不正确",
		"control_arr.required":      "请输入控制字段",
	}
}

func (r *UserRequest) Attributes(ctx http.Context) map[string]string {
	return map[string]string{

		"name": "姓名",

		"mobile": "手机号",

		"password": "密码",

		"confirm_password": "确认密码",

		"area": "地区",

		"contact": "联系人",

		"contact_mobile": "联系人手机号",

		"address": "详细地址",

		"id_card": "身份证号",

		"control_arr": "控制字段",
	}
}

func (r *UserRequest) PrepareForValidation(ctx http.Context, data validation.Data) error {
	if name, exist := data.Get("pid"); exist {
		return data.Set("pid", cast.ToInt(name))
	}
	return nil
}
